//
//  AEGoodsDetailInfoView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/14.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEGoodsDetailInfoView.h"
#import "AE_Solar-Swift.h"
#import "AEGoodsDetailViewModel.h"

@interface AEGoodsDetailInfoView ()

@property (nonatomic, strong) AEGoodsDetailViewModel *viewModel;

@property (nonatomic, strong) UIImageView *thumalImgView;

@property (nonatomic, strong) CALayer *line;

/// 积分
@property (nonatomic, strong) UILabel *cntLabel;

/// 商品名称
@property (nonatomic, strong) UILabel *nameLabel;

/// 剩余数量
@property (nonatomic, strong) UILabel *remainingNumLabel;

@end

@implementation AEGoodsDetailInfoView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = kWhiteColor;
}

- (void)initWithThumalImgView {
    UIImage *defaultImg = [UIImage imageNamed:@"placeholder_image"];
    if (!_thumalImgView) {
        CGRect frame = CGRectMake(0, 0, self.width, 166);
        _thumalImgView = [UIImageView createWithImageView:self frame:frame defaultImage:defaultImg propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
        }];
    }
    if (self.viewModel && self.viewModel.goodsInfoModel) {
        [_thumalImgView sd_setImageWithURL:[NSURL URLWithString:self.viewModel.goodsInfoModel.productImgUrl] placeholderImage:defaultImg];
    }
}

- (void)initWithLine {
    if (!_line) {
        CGRect frame = CGRectMake(15, self.height - 10, self.width - 30, 1);
        _line = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EBEBEB"]];
    }
}

- (void)initWithCntLabel {
    if (!_cntLabel) {
        CGRect frame = CGRectMake(0, self.thumalImgView.bottom + 19, 0, 36);
        _cntLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#F6020A"];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor colorWithHexString:@"#fed6d7"];
            label.layer.cornerRadius = frame.size.height * 0.5;
            label.layer.masksToBounds = YES;
        }];
    }
    if (self.viewModel && self.viewModel.goodsInfoModel) {
        NSString *cnt = [AEStringExten localizedValueWithKey:@"积分"];
        NSString *title = [NSString stringWithFormat:@"%zd%@", self.viewModel.goodsInfoModel.productScore, cnt];
        float width = [title widthForFont:_cntLabel.font] + 2 + 30;
        self.cntLabel.width = width;
        self.cntLabel.left = self.width - width - 15;
        self.cntLabel.text = title;
    }
}

/// 商品名称
- (void)initWithNameLabel {
    if (!_nameLabel) {
        CGRect frame = CGRectMake(15, self.thumalImgView.bottom + 12, self.cntLabel.left - 15 - 10, 26);
        _nameLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:19 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.viewModel && self.viewModel.goodsInfoModel) {
        self.nameLabel.text = objectNull(self.viewModel.goodsInfoModel.productName);
    }
}

/// 剩余数量
- (void)initWithRemainingNumLabel {
    if (!_remainingNumLabel) {
        CGRect frame = CGRectMake(15, self.nameLabel.bottom + 3, 0, 17);
        _remainingNumLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:11 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#303939"];
            label.layer.cornerRadius = 2;
            label.layer.masksToBounds = YES;
            label.backgroundColor = [UIColor colorWithHexString:@"#F4F7F8"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    if (self.viewModel && self.viewModel.goodsInfoModel) {
        NSString *tmp = [AEStringExten localizedValueWithKey:@"剩余"];
        NSString *title = [NSString stringWithFormat:@"%@%zd", tmp, self.viewModel.goodsInfoModel.productCount];
        float width = [title widthForFont:self.remainingNumLabel.font] + 2 + 16;
        self.remainingNumLabel.width = width;
        self.remainingNumLabel.text = title;
    }
}

- (void)updateWithViewData:(id)data {
    
    self.viewModel = data;
    
    [self initWithThumalImgView];
    
    [self initWithLine];
    
    [self initWithCntLabel];
    
    [self initWithNameLabel];
    
    [self initWithRemainingNumLabel];
}

@end
